import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {FormControl, FormGroup} from '@angular/forms';
import {ArticleListService} from '../services/article-list.service';

@Component({
  selector: 'app-article',
  templateUrl: './article.component.html',
  styleUrls: ['./article.component.css']
})
export class ArticleComponent implements OnInit {
  articleId: string;
  parentArticleCategoryId: string;
  formModel: FormGroup;

  constructor(
    private routeInfo: ActivatedRoute,
    private articleListService: ArticleListService,
    private router: Router) {
    this.formModel = new FormGroup({
      title: new FormControl(),
      content: new FormControl()
    });
    this.parentArticleCategoryId = this.routeInfo.snapshot.queryParams.parentArticleCategoryId;
  }

  ngOnInit() {
  }

  onSubmit() {
    this.articleListService.addArticle({
      articleCategoryId: this.parentArticleCategoryId,
      title: this.formModel.value.title,
      content: this.formModel.value.content,
      pushTime: Math.round(new Date().getTime() / 1000),
      useLinkUrl: 0,
      shareState: 2,
      isAllowedComment: 1,
      isAllowedLike: 1,
      typeId: -1
    }).subscribe(data => {
      if (data) {
        this.router.navigate(['/articles', this.parentArticleCategoryId]);
      }
    });
  }
}
